/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemPoet.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.poemPoet.domain.PoemPoet;
import me.zhengjie.poemPoet.domain.vo.domain.vo.PoemPoetQueryCriteria;
import me.zhengjie.poemPoet.mapper.PoemPoetMapper;
import me.zhengjie.poemPoet.service.PoemPoetService;
import me.zhengjie.poemmanager.domain.vo.QueryCriteria;
import me.zhengjie.poemmanager.mapper.CommentMapper;
import me.zhengjie.poemmanager.mapper.FavoritesMapper;
import me.zhengjie.poemmanager.mapper.ObjectLikeMapper;
import me.zhengjie.poemmanager.util.ExtractUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author hwl
* @date 2024-12-08
**/
@Service
@Slf4j
@RequiredArgsConstructor
public class PoemPoetServiceImpl extends ServiceImpl<PoemPoetMapper, PoemPoet> implements PoemPoetService {

    private final PoemPoetMapper poemPoetMapper;
    private final ObjectLikeMapper objectLikeMapper;
    private final CommentMapper commentMapper;
    private final FavoritesMapper favoritesMapper;
    @Override
    public PageResult<PoemPoet> queryAll(PoemPoetQueryCriteria criteria, Page<Object> page) {
        StopWatch stopWatch = new StopWatch("PoemPoet查询性能分析");
        // 主查询耗时
        stopWatch.start("分页查询主数据");
        IPage<PoemPoet> poemPoetIPage = poemPoetMapper.findAll(criteria, page);
        stopWatch.stop();
        return PageUtil.toPage(poemPoetIPage);
    }

    @Override
    public List<PoemPoet> queryAll(PoemPoetQueryCriteria criteria){
        return poemPoetMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PoemPoet resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PoemPoet resources) {
        PoemPoet poemPoet = getById(resources.getPoemId());
        poemPoet.copy(resources);
        saveOrUpdate(poemPoet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<PoemPoet> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PoemPoet poemPoet : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", poemPoet.getTitle());
            map.put("作者id（用户的id或者作者的id）", poemPoet.getAuthorId());
            map.put("朝代", poemPoet.getDynasty());
            map.put("主体内容", poemPoet.getContent());
            map.put("创造时间", poemPoet.getCreatedAt());
            map.put("更新时间", poemPoet.getUpdatedAt());
            map.put("作者姓名", poemPoet.getAuthorName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<QueryCriteria> queryByTitle(String title) {
        return poemPoetMapper.queryByTitle(title);
    }

    @Override
    public List<PoemPoet> getALl() {
        List<PoemPoet>poems=poemPoetMapper.getALl();
        for (PoemPoet poem:
                poems) {
//            poem.setFavoriteCount(favoritesMapper.getFavoriteCount(poem.getPoemId(),2));
//            poem.setComments(commentMapper.getCommentByObjectId(poem.getPoemId(),2));
//            poem.setCommentCount(objectLikeMapper.getLikeCount(poem.getPoemId(),2));
//            poem.setLikeCount(objectLikeMapper.getLikeCount(poem.getPoemId(),2));
            String content= ExtractUtil.extrac(poem.getContent()).replaceAll("\\s*|\r|\n|\t","");
            if(content.length()>50)
                content=content.substring(0,50);
            poem.setContent(content);
        }
        return poems;
    }

    @Override
    public List<PoemPoet> getMyFavoritePoem(String userName) {
        List<PoemPoet>poems=poemPoetMapper.getMyFavoritePoem(userName);
        for (PoemPoet poem:
                poems) {
//            poem.setFavoriteCount(favoritesMapper.getFavoriteCount(poem.getPoemId(),2));
//            poem.setCommentCount(commentMapper.getCommentCount(poem.getPoemId(),2));
//            poem.setLikeCount(objectLikeMapper.getLikeCount(poem.getPoemId(),2));
            String content= ExtractUtil.extrac(poem.getContent()).replaceAll("\\s*|\r|\n|\t","");
            if(content.length()>50)
                content=content.substring(0,50);
            poem.setContent(content);
        }
        return poems;
    }

    @Override
    public PoemPoet getPoemPoetById(Integer poemId) {
        PoemPoet p = poemPoetMapper.getById(poemId);
        p.setComments(commentMapper.getPoemPoetCommentByObjectId(poemId));
        return p;
    }
}